﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.auth.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;
import static com.hh.bss.util.extjs.JsonHelper.outJsonArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.JsonHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class SystemBranchTitleController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private SystemBranchTitleService systemBranchTitleService;
	
	private SystemBranchTitle systemBranchTitle;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setSystemBranchTitleServiceImpl(SystemBranchTitleService service) {
		this.systemBranchTitleService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	public void parenttitlelist(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String deptId=request.getParameter("deptId");
		//StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		//sql.append(" select t2.id,t2.name from system_branch_title t2 where exists(select 1 from (select t1.parent_ids from system_branch_title t1 where t1.dept_id in(select d1.parent_id from system_branch_dept d1 where d1.id=?)) aa where aa.parent_ids like concat(t2.parent_ids,'%'))  ");
		
		values.add(StringHelper.parseInteger(deptId, 0));
		
		//List l=systemBranchTitleService.getEntityDao().getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());
		List l=new ArrayList();
		
		List lDept=systemBranchTitleService.getEntityDao().getHibernateTemplate().find("from SystemBranchDept t where t.id=? ", StringHelper.parseInteger(deptId, 0));
		
		
		
		
		if (lDept!=null&&lDept.size()>0){
			SystemBranchDept systemBranchDept=(SystemBranchDept)lDept.get(0);
			List lpDept=systemBranchTitleService.getEntityDao().getHibernateTemplate().find("from SystemBranchDept t where t.id=? ", systemBranchDept.getParentId());
			
			if (lpDept!=null&&lpDept.size()>0){
				SystemBranchDept pSystemBranchDept=(SystemBranchDept)lpDept.get(0);
				if (!StringHelper.isEmpty(pSystemBranchDept.getParentIds())){
					String[] depts=pSystemBranchDept.getParentIds().split("/");
					
					if (depts!=null&&depts.length>0){
						String dId=null;
						//取 最近的上级的岗位 
						for(int i=depts.length-1;i>=0;i--){						
							dId=depts[i];
						   if (StringHelper.isEmpty(dId)) continue;
						   StringBuffer querySql= new StringBuffer();
						   List pvalues = new ArrayList();
						   pvalues.add(StringHelper.parseInteger(dId, 0));
						   querySql.append(" select t2.id,t2.name from system_branch_title t2 where t2.dept_id=? ");
						   //System.out.println(depts.length+"--"+i+"**********:"+dId);
						   l=systemBranchTitleService.getEntityDao().getJdbcDao().findByQuery(querySql.toString(),pvalues.toArray(), new MapObjectRowMapper());
						   if (l!=null&&l.size()>0) break;
						}
					}
				}
			}
			//处理顶级岗位
			List lCompany=systemBranchTitleService.getEntityDao().getHibernateTemplate().find("from SystemBranchCompany t where t.id=? ", systemBranchDept.getBranchCompanyId());
			if (lCompany!=null&&lCompany.size()>0){
				SystemBranchCompany systemBranchCompany=(SystemBranchCompany)lCompany.get(0);
				if (systemBranchCompany.getParentId()!=null&&systemBranchCompany.getParentId().intValue()==0){
					if (l==null||l.size()<1){	
						Map m=new HashMap();
						m.put("id", new Integer(0));
						m.put("name", "无");					
						l.add(m);
					}
				}
			}	
		}
		
		if (l==null) l=new ArrayList();
		
//		if (l==null||l.size()<1){
//			Map m=new HashMap();
//			m.put("id", new Integer(0));
//			m.put("name", "根岗位");
//			
//			l.add(m);
//		}
		JsonHelper.outJsonString(response,JSONArray.fromObject(l).toString());
	}

	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,SystemBranchTitleQuery systemBranchTitleQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = systemBranchTitleService.findPage(systemBranchTitleQuery);
		
		List<SystemBranchTitle> SystemBranchTitlelist = (List) page.getResult();
		ListRange<SystemBranchTitle> resultList = new ListRange<SystemBranchTitle>();
		resultList.setRows(SystemBranchTitlelist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void titlelist(HttpServletRequest request,HttpServletResponse response,SystemBranchTitleQuery systemBranchTitleQuery) throws IOException
	{
		List titlelist = (List)systemBranchTitleService.findSystemBranchTitle(systemBranchTitleQuery);
		outJsonArray(response,titlelist);
		
	}
	public void get(HttpServletRequest request,HttpServletResponse response,SystemBranchTitleQuery systemBranchTitleQuery) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			SystemBranchTitle oldSystemBranchTitle = (SystemBranchTitle)systemBranchTitleService.getById(systemBranchTitleQuery.getId());
                         
			//取父节点名称 
			List pTitle=systemBranchTitleService.getEntityDao().getHibernateTemplate().find("from SystemBranchTitle t where t.id=? ", oldSystemBranchTitle.getParentId());
			if (pTitle!=null&&pTitle.size()>0){
				SystemBranchTitle systemBranchTitle=(SystemBranchTitle)pTitle.get(0);
				oldSystemBranchTitle.setParentName(systemBranchTitle.getName());
			}else oldSystemBranchTitle.setParentName("无");
			if(oldSystemBranchTitle!=null) result.put("data", oldSystemBranchTitle);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,SystemBranchTitle systemBranchTitle) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchTitle.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(systemBranchTitle,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchTitle.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(systemBranchTitle,"createDt",new Date());}catch(Exception ex){}
				}

			systemBranchTitleService.save(systemBranchTitle);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,SystemBranchTitle systemBranchTitle) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        SystemBranchTitle oldsystemBranchTitle = (SystemBranchTitle)systemBranchTitleService.getById(systemBranchTitle.getId());
                         
			bind(request,oldsystemBranchTitle);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchTitle.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldsystemBranchTitle,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchTitle.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldsystemBranchTitle,"modifiedDt",new Date());}catch(Exception ex){}
				}


			systemBranchTitleService.update(oldsystemBranchTitle);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				systemBranchTitleService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
